package Projektarbete;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class ChooseStdDirectory extends JPanel implements ActionListener {
	JFileChooser chooseDir;
	JTextField showDir;
	JButton browseDir;

	public ChooseStdDirectory() {
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		showDir = new JTextField();
		showDir.setEditable(false);
		showDir.setMinimumSize(new Dimension(300, 50));
		showDir.setPreferredSize(new Dimension(200, 20));
		showDir.setBackground(Color.WHITE);
		add(showDir);
		readFromFile();
		browseDir = new JButton("Browse");
		browseDir.addActionListener(this);
		add(browseDir);
	}
	
	public void writeToFile(String dir){
		FileOutputStream fout;		

		try
		{
		    // Open an output stream
		    fout = new FileOutputStream ("src/stddir.txt");

		    // Print a line of text
		    new PrintStream(fout).println (dir);

		    // Close our output stream
		    fout.close();		
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to write to file");
			System.exit(-1);
		}
	}

	public void readFromFile() {
		String out;
		FileInputStream fin;

		try {
			// Open an input stream
			fin = new FileInputStream("src/stddir.txt");

			out = new DataInputStream(fin).readLine();

			showDir.setText(out);
			fin.close();
		}
		// Catches any error conditions
		catch (IOException e) {
			System.out.println("filen finns inte");
		}

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		chooseDir = new JFileChooser();
		chooseDir.setCurrentDirectory(new java.io.File("C:/"));
		chooseDir.setDialogTitle("Choose standard directory");
		chooseDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		if (chooseDir.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
			showDir.setText("" + chooseDir.getSelectedFile());
			writeToFile(""+chooseDir.getSelectedFile());
		} else {
			System.out.println("No Selection ");
		}
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame("");
		ChooseStdDirectory panel = new ChooseStdDirectory();

		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		frame.getContentPane().add(panel, "Center");

		frame.setSize(310, 80);
		frame.setVisible(true);
	}

}
